package cannot.backcontroller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cannot.pojo.MainType;
import cannot.pojo.OtherType;
import cannot.service.MainTypeService;
import cannot.service.OtherTypeService;
/**
 * 管理员，分类管理
 * @author Administrator
 *
 */
@Controller
public class SubCategoryController {
	@Autowired
	MainTypeService mainTypeService;
	@Autowired
	OtherTypeService otherTypeService;
	
	/**
	 * 属性管理→属性管理
	 * @return
	 */
	@RequestMapping("admin_rightContent_subCategory")
	public String AdminRightContentSubCategory(Model model){
		List<MainType> mainTypes = mainTypeService.listMainType();
		model.addAttribute("mainTypes", mainTypes);
		 return "admin/rightContent/subCategory";
	}
	

	/**
	 * 增加主分类
	 */
	@RequestMapping("admin_subCategory_addMaintype")
	@ResponseBody
	@Transactional(rollbackFor = { Exception.class })
	public Object addMainType(@RequestParam("descri") String descri){
		MainType mainType = new MainType();
		mainType.setDescri(descri);
		mainTypeService.insertMainType(mainType);
		return mainType;
	}
	/**
	 * 更新主分类
	 */
	@RequestMapping("admin_subCategory_updateMaintype")
	@ResponseBody
	@Transactional(rollbackFor = { Exception.class })
	public Object updateMainType(@RequestParam("descri") String descri,@RequestParam("id") Integer id){
		MainType mainType = new MainType();
		mainType.setDescri(descri);
		mainType.setId(id);
		mainTypeService.updateMainType(mainType);
		return mainType;
	}
	/**
	 * 删除主分类
	 */
	@RequestMapping("admin_subCategory_deleteMaintype")
	@ResponseBody
	@Transactional(rollbackFor = { Exception.class })
	public Object deleteMainType(@RequestParam("id") Integer id){
		mainTypeService.deleteMainType(id);
		MainType mainType = new MainType();
		return mainType;
	}
	
	/**
	 * 查询分区分类
	 * @param id 主分类id
	 * @return
	 */
	@RequestMapping("admin_subCategory_listOtherType")
	@ResponseBody
	public Object listOtherType(Integer id){
		List<OtherType> otherTypes =otherTypeService.listByMainType(id); 
		return otherTypes;
	}
	/**
	 * 增加分区分类
	 * @param descri
	 * @param foreginkey 分群分类的外键
	 * @return
	 */
	@RequestMapping("admin_subCategory_addOthertype")
	@ResponseBody
	@Transactional(rollbackFor = { Exception.class })
	public Object addOtherType(@RequestParam("descri") String descri,@RequestParam("foreginkey") Integer foreginkey){
		OtherType otherType = new OtherType();
		otherType.setForeginkey(foreginkey);
		otherType.setDescri(descri);
		otherTypeService.insertOtherType(otherType);
		return otherType;
	}
	
	/**
	 * 更新分区分类
	 */
	@RequestMapping("admin_subCategory_updateOthertype")
	@ResponseBody
	@Transactional(rollbackFor = { Exception.class })
	public Object updateOtherType(@RequestParam("descri") String descri,@RequestParam("id") Integer id,@RequestParam("foreginkey") Integer foreginkey){
		OtherType otherType = new OtherType();
		otherType.setForeginkey(foreginkey);
		otherType.setDescri(descri);
		otherType.setId(id);
		otherTypeService.updateOtherType(otherType);
		return otherType;
	}
	/**
	 * 删除分区分类
	 */
	@RequestMapping("admin_subCategory_deleteOthertype")
	@ResponseBody
	@Transactional(rollbackFor = { Exception.class })
	public Object deleteOtherType(@RequestParam("id") Integer id){
		otherTypeService.deleteOtherType(id);
		OtherType otherType = new OtherType();
		return otherType;
	}
	
}
